﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Utils;
namespace UI.ModSalary
{
    public partial class SalaryAdd : System.Web.UI.Page
    {
        DefDicBiz defineDictionary = new DefDicBiz();
        SetsItemBiz setsItemBiz = new SetsItemBiz();
        SalaryTableBiz salaryTableBiz = new SalaryTableBiz();
        string empCode = "";
        string setsID = "";
        string strYear = "";
        string strMonth = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["EmpCode"] != null)
            {
                empCode = Request.Params["EmpCode"].ToString();
                setsID = Request.Params["SetsID"].ToString();
                strYear = Request.Params["StrYear"].ToString();
                strMonth = Request.Params["StrMonth"].ToString();
            }
            if (empCode == "" || strYear == "" || strMonth == "")
            {
                JSUtility.Alert(this, "参数错误 ");
                return;
            }
            //从生成的工资簿中取
            DataTable baseDt = salaryTableBiz.GetOneInfo(empCode, strYear, strMonth);
            if (!IsPostBack)
            {
                lbYear.Text = strYear;
                lbMonth.Text = strMonth;
                int row = baseDt.Rows.Count;
                if (row > 0)
                {
                    lbEmpCode.Text = baseDt.Rows[0]["EmpCode"].ToString();
                    lbDeptName.Text = baseDt.Rows[0]["DeptName"].ToString();
                    lbDutyName.Text = baseDt.Rows[0]["DutyName"].ToString();
                    lbEmpName.Text = baseDt.Rows[0]["EmpName"].ToString();
                    lbAreaName.Text = baseDt.Rows[0]["AreaName"].ToString();
                    lbStateName.Text = baseDt.Rows[0]["StateName"].ToString();
                    string joinTime = baseDt.Rows[0]["JoinTime"].ToString();
                    if (Utils.RegexRule.IsDate(joinTime))
                        lbJoinTime.Text = Convert.ToDateTime(joinTime).ToString("yyyy-MM-dd");
                    Memo.Text = baseDt.Rows[0]["Memo"].ToString();
                    btnAdd.Enabled = false;
                }
                else
                {
                    EmployeeBiz employeeBiz = new EmployeeBiz();
                    if (!string.IsNullOrWhiteSpace(empCode))
                    {

                        DataTable dt = employeeBiz.GetOneInfo(empCode);
                        if (dt.Rows.Count > 0)
                        {
                            lbEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                            lbDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                            lbDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                            lbEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                            lbAreaName.Text = dt.Rows[0]["AreaName"].ToString();
                            lbStateName.Text = dt.Rows[0]["StateName"].ToString();
                            string joinTime = dt.Rows[0]["JoinTime"].ToString();
                            if (Utils.RegexRule.IsDate(joinTime))
                                lbJoinTime.Text = Convert.ToDateTime(joinTime).ToString("yyyy-MM-dd");

                        }
                        else
                        {
                            JSUtility.Alert(this, "不存在该员工编号: " + empCode);
                            return;
                        }
                       // btnUpdate.Enabled = false;
                       // btnDelete.Enabled = false;
                        btnNext.Enabled = false;
                        btnPre.Enabled = false;
                    }

                }
            }
            if (baseDt.Rows.Count <= 0) //如果没有本年本月工资数据，就从个人工资项初始设置里面获取值作为初始值
                baseDt = new EmpItemBiz().GetOneInfo(empCode);
            FillBaseInfo(empCode, setsID, baseDt);
        }

        protected void FillBaseInfo(string empCode, string setsID, DataTable baseDt)
        {

            if (setsID == "" || setsID == "0")
                setsID = new EmpSetsBiz().GetSetsID(empCode);
            InitTextBox(empCode, setsID, baseDt);

        }

        private void InitTextBox(string empCode, string setsID, DataTable baseDt)
        {
            if (setsID != "")
            {
                Panel1.Controls.Add(new LiteralControl("<table style='width: 80%;' align=center class=td>"));
                Panel1.Controls.Add(new LiteralControl("\n"));
                DataTable dt = setsItemBiz.GetItemDict(setsID);

                //获取员工基本设置
                //DataTable baseDt= empItemBiz.GetOneInfo(empCode);
                int row = baseDt.Rows.Count;
                //if (row > 0)
                //{
                //    defineDictionary.InitDropDownList(ddlSalGade, baseDt.Rows[0]["SalGade"].ToString().Trim());
                //   // defineDictionary.InitSynDropDownList(ddlSalGade, baseDt.Rows[0]["SalGade"].ToString().Trim());
                //    HourSalary.Text = baseDt.Rows[0]["HourSalary"].ToString();
                //    BankName.Text = baseDt.Rows[0]["BankName"].ToString();
                //    BankAccount.Text = baseDt.Rows[0]["BankAccount"].ToString();
                //    btnAdd.Enabled = false;
                //}
                //else
                //{
                //    defineDictionary.InitDropDownList("Wage_Gade", ddlSalGade, "A");
                //    btnUpdate.Enabled = false;
                //    btnDelete.Enabled = false;
                //}
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Panel1.Controls.Add(new LiteralControl("<tr>"));
                    Panel1.Controls.Add(new LiteralControl(" <td style='width:75px;'>"));
                    TableCell cell = new TableCell();//创建单元格,也就是第一列
                    cell.Text = dt.Rows[i]["itemName"].ToString();//设置单元格内的文本
                    Panel1.Controls.Add(cell);
                    Panel1.Controls.Add(new LiteralControl("</td>"));
                    Panel1.Controls.Add(new LiteralControl(" <td >"));
                    TextBox textBox = new TextBox();
                    textBox.ID = dt.Rows[i]["WageItem"].ToString();
                    textBox.CssClass = "InputCss";
                    if (row > 0)
                        textBox.Text = baseDt.Rows[0][dt.Rows[i]["WageItem"].ToString()].ToString();
                    Panel1.Controls.Add(textBox);

                    Panel1.Controls.Add(new LiteralControl("</td>"));

                    Panel1.Controls.Add(new LiteralControl("</tr>"));
                    Panel1.Controls.Add(new LiteralControl("\n"));
                }

                Panel1.Controls.Add(new LiteralControl("</table>"));
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {

            if (salaryTableBiz.IsMonthEnd(strYear, strMonth, setsID))
            {
                JSUtility.Alert(this, "该月工资簿已经月结，不能增加");
                return;

            }
            if (string.IsNullOrWhiteSpace(empCode))
            {
                Utils.JSUtility.Alert(this, "工号不能为空");
                return;
            }
            if (!new EmployeeBiz().IsExistEmpCode(empCode))
            {
                Utils.JSUtility.Alert(this, "该工号没有员工使用");
                return;
            } //
            if (salaryTableBiz.IsExistOneInfo(empCode, strYear, strMonth))
            {
                Utils.JSUtility.Alert(this, "只可以更新");
                return;
            }
            //判断有没有工资方案
            /*没有工资方案 也能增加工资
            EmpItemBiz empItemBiz = new EmpItemBiz();
            if (!empItemBiz.IsExistEmpCode(empCode, setsID))
            {                //判断有没有工资帐套，没有则插入
                EmpSetsBiz empSetsBiz = new EmpSetsBiz();
                if (!empSetsBiz.IsExistEmpCode(empCode))
                    empSetsBiz.ConfigSets(empCode, "", setsID);

                //没有工资基本方案则插入
                //加入个人工资项
                SortedDictionary<string, string> sdList0 = new SortedDictionary<string, string>();
                sdList0.Add("EmpCode", empCode);
                sdList0.Add("SalGade", "C");

                foreach (System.Web.UI.Control control in this.Form.Controls)
                {
                    FindSelecedControl(control, sdList0);
                }
                if (!empItemBiz.Insert("Wage_EmpItem", sdList0))
                {
                    JSUtility.Alert(this, "员工基本工资项插入失败，所以该月工资插入失败");
                    return;
                }
            }
            */

            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("EmpCode", empCode);
            sdList.Add("SalYear", strYear);
            sdList.Add("SalMonth", strMonth);
            sdList.Add("SetsID", setsID);


            foreach (System.Web.UI.Control control in this.Form.Controls)
            {
                FindSelecedControl(control, sdList);
            }
            defineDictionary.Insert("Wage_SalaryList", sdList);
            ReCau(setsID,empCode,strYear,strMonth);
            JSUtility.Alert(this, "增加员工工资信息成功");

        }
        //重新计算公式项
        private void ReCau(string setsID,string empCode,string strYear,string strMonth)
        {
            //从帐套项目信息中提取计算项，及计算公式 setsID

            //然后更新改行数据 empCode,strYear,strMonth
            salaryTableBiz.ReCau(setsID, empCode, strYear, strMonth);
        }
        private void FindSelecedControl(Control control, SortedDictionary<string, string> sdList)
        {
            if (control is TextBox)
            {
                TextBox txt = (TextBox)control;
                string value = txt.Text.Trim();
                if (!string.IsNullOrWhiteSpace(value))
                    sdList.Add((control as TextBox).ID, value);

            }
            else
            {
                for (int i = 0; i < control.Controls.Count; i++)
                {
                    FindSelecedControl(control.Controls[i], sdList);
                }
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (salaryTableBiz.IsMonthEnd(strYear, strMonth, setsID))
            {
                JSUtility.Alert(this, "该月工资簿已经月结，不能更新");
                return;

            }
            if (string.IsNullOrWhiteSpace(empCode))
            {
                Utils.JSUtility.Alert(this, "工号不能为空");
                return;
            }
            if (!salaryTableBiz.IsExistOneInfo(empCode, strYear, strMonth))
            {
                Utils.JSUtility.Alert(this, "只可以增加");
                return;
            }
            //根据年月工号 找到ID
            string salID = salaryTableBiz.GetSalID(empCode, strYear, strMonth);
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("SalId", salID);
            foreach (System.Web.UI.Control control in this.Form.Controls)
            {
                FindSelecedControl(control, sdList);
            }
            defineDictionary.Update("Wage_SalaryList", sdList);
            ReCau(setsID, empCode, strYear, strMonth);
            JSUtility.Alert(this, "修改员工工资信息成功");

        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (salaryTableBiz.IsMonthEnd(strYear, strMonth, setsID))
            {
                JSUtility.Alert(this, "该月工资簿已经月结，不能删除");
                return;

            }
            if (salaryTableBiz.DeleteOneInfo(empCode, strYear, strMonth))
            { JSUtility.Alert(this, "删除工资信息成功"); }
            else
                JSUtility.Alert(this, "删除工资信息失败");
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            JSUtility.CloseWindow();
        }

        protected void btnPre_Click(object sender, EventArgs e)
        {
            
            string strCode = salaryTableBiz.GetNextOrPre(strYear, strMonth, empCode,"Pre");
            if (strCode!="0")
            Response.Redirect("SalaryAdd.aspx?EmpCode=" + strCode + "&StrYear=" + strYear + "&StrMonth=" + strMonth + "&SetsID=" + setsID);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            string strCode = salaryTableBiz.GetNextOrPre(strYear, strMonth, empCode, "Next");
            if (strCode != "0")
            Response.Redirect("SalaryAdd.aspx?EmpCode=" + strCode + "&StrYear=" + strYear + "&StrMonth=" + strMonth + "&SetsID=" + setsID);
        }
    }
}